﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace LHS.Core.CaseRecord.Classes
{
    public class clsUtils
    {
        /// <summary>
        /// Return the string with length equals 20 chars (with format YYYYMMDDHHMMSSxxxxxx)
        /// </summary>
        /// <returns></returns>
        public static String getRandomString()
        {
            String strReturn = "";
            DateTime dtNow = DateTime.Now;
            strReturn += dtNow.Year;
            strReturn += dtNow.Month < 10 ? "0" + dtNow.Month.ToString() : dtNow.Month.ToString();
            strReturn += dtNow.Day < 10 ? "0" + dtNow.Day.ToString() : dtNow.Day.ToString();
            strReturn += dtNow.Hour < 10 ? "0" + dtNow.Hour.ToString() : dtNow.Hour.ToString();
            strReturn += dtNow.Minute < 10 ? "0" + dtNow.Minute.ToString() : dtNow.Minute.ToString();
            strReturn += dtNow.Second < 10 ? "0" + dtNow.Second.ToString() : dtNow.Second.ToString();
            strReturn += dtNow.Millisecond < 10 ? "00" + dtNow.Millisecond.ToString() : dtNow.Millisecond < 100 ? "0" + dtNow.Millisecond.ToString() : dtNow.Millisecond.ToString();
            int intRandom = (new Random()).Next(1000);
            strReturn += intRandom < 10 ? "00" + intRandom.ToString() : intRandom < 100 ? "0" + intRandom.ToString() : intRandom.ToString(); ;
            return strReturn;
        }

        public static String getNameFromString(String strSource, Boolean blnIsTableName)
        {
            String strReturn = "";
            for (int i = 0; i < strSource.Length; i++)
            {
                switch (strSource[i])
                {
                    //__________A___________
                    case 'Á':
                    case 'À':
                    case 'Ả':
                    case 'Ã':
                    case 'Ạ':

                    case 'Â':
                    case 'Ấ':
                    case 'Ầ':
                    case 'Ẩ':
                    case 'Ẫ':
                    case 'Ậ':

                    case 'Ă':
                    case 'Ắ':
                    case 'Ằ':
                    case 'Ẳ':
                    case 'Ẵ':
                    case 'Ặ':
                        strReturn += 'A';
                        break;

                    //__________E___________
                    case 'É':
                    case 'È':
                    case 'Ẻ':
                    case 'Ẽ':
                    case 'Ẹ':

                    case 'Ê':
                    case 'Ế':
                    case 'Ề':
                    case 'Ể':
                    case 'Ễ':
                    case 'Ệ':
                        strReturn += 'E';
                        break;

                    //__________I___________
                    case 'Í':
                    case 'Ì':
                    case 'Ỉ':
                    case 'Ĩ':
                    case 'Ị':
                        strReturn += 'I';
                        break;

                    //__________O___________
                    case 'Ó':
                    case 'Ò':
                    case 'Ỏ':
                    case 'Õ':
                    case 'Ọ':

                    case 'Ô':
                    case 'Ố':
                    case 'Ồ':
                    case 'Ổ':
                    case 'Ỗ':
                    case 'Ộ':

                    case 'Ơ':
                    case 'Ớ':
                    case 'Ờ':
                    case 'Ở':
                    case 'Ỡ':
                    case 'Ợ':
                        strReturn += 'O';
                        break;

                    //__________U___________
                    case 'Ú':
                    case 'Ù':
                    case 'Ủ':
                    case 'Ũ':
                    case 'Ụ':

                    case 'Ư':
                    case 'Ứ':
                    case 'Ừ':
                    case 'Ử':
                    case 'Ữ':
                    case 'Ự':
                        strReturn += 'U';
                        break;

                    //__________y___________
                    case 'Ý':
                    case 'Ỳ':
                    case 'Ỷ':
                    case 'Ỹ':
                    case 'Ỵ':
                        strReturn += 'y';
                        break;

                    //__________D___________
                    case 'Đ':
                        strReturn += 'D';
                        break;

                    //__________a___________
                    case 'á':
                    case 'à':
                    case 'ả':
                    case 'ã':
                    case 'ạ':

                    case 'â':
                    case 'ấ':
                    case 'ầ':
                    case 'ẩ':
                    case 'ẫ':
                    case 'ậ':

                    case 'ă':
                    case 'ắ':
                    case 'ằ':
                    case 'ẳ':
                    case 'ẵ':
                    case 'ặ':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'A';
                        else
                            strReturn += 'a';
                        break;

                    //__________e___________
                    case 'é':
                    case 'è':
                    case 'ẻ':
                    case 'ẽ':
                    case 'ẹ':

                    case 'ê':
                    case 'ế':
                    case 'ề':
                    case 'ể':
                    case 'ễ':
                    case 'ệ':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'E';
                        else
                            strReturn += 'e';
                        break;

                    //__________i___________
                    case 'í':
                    case 'ì':
                    case 'ỉ':
                    case 'ĩ':
                    case 'ị':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'I';
                        else
                            strReturn += 'i';
                        break;

                    //__________o___________
                    case 'ó':
                    case 'ò':
                    case 'ỏ':
                    case 'õ':
                    case 'ọ':

                    case 'ô':
                    case 'ố':
                    case 'ồ':
                    case 'ổ':
                    case 'ỗ':
                    case 'ộ':

                    case 'ơ':
                    case 'ớ':
                    case 'ờ':
                    case 'ở':
                    case 'ỡ':
                    case 'ợ':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'O';
                        else
                            strReturn += 'o';
                        break;

                    //__________u___________
                    case 'ú':
                    case 'ù':
                    case 'ủ':
                    case 'ũ':
                    case 'ụ':

                    case 'ư':
                    case 'ứ':
                    case 'ừ':
                    case 'ử':
                    case 'ữ':
                    case 'ự':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'U';
                        else
                            strReturn += 'u';
                        break;

                    //__________y___________
                    case 'ý':
                    case 'ỳ':
                    case 'ỷ':
                    case 'ỹ':
                    case 'ỵ':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'Y';
                        else
                            strReturn += 'y';
                        break;

                    //__________d___________
                    case 'đ':
                        if (i == 0 || strSource[i - 1] == ' ')
                            strReturn += 'D';
                        else
                            strReturn += 'd';
                        break;

                    default:
                        if ((strSource[i] >= '0' && strSource[i] <= '9') || (strSource[i] >= 'a' && strSource[i] <= 'z') || (strSource[i] >= 'A' && strSource[i] <= 'Z') || strSource[i] == '_')
                            if (i == 0 || strSource[i - 1] == ' ')
                                strReturn += strSource[i].ToString().ToUpper();
                            else
                                strReturn += strSource[i];
                        break;
                }
            }
            if (blnIsTableName)
                return "table" + strReturn;
            return "col" + strReturn;
        }

        public static void msgHandler(Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }
}
